package com.team7.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.service.CourseListService;
import com.team7.dao.CourseCheckDao;
import com.team7.dao.CourseDao;
import com.team7.dao.CourseSubjectDao;
import com.team7.entity.CourseCheck;
import com.team7.entity.Subject;
import com.team7.service.CourseCheckService;
import com.team7.vo.CommonResult;
import com.team7.vo.CourseCheckVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.team7.service.impl.CourseListServiceImpl.findcourseSubjectName;

/**
 * @program: team7-object
 * @description:
 * @author: 田宏洋
 * @create: 2021-10-20 16:42
 **/
@Service
public class CourseCheckServiceImpl extends ServiceImpl<CourseCheckDao, CourseCheck> implements CourseCheckService {
    @Resource
    private CourseCheckDao courseCheckDao;
    @Resource
    private CourseSubjectDao courseSubjectDao;
    @Override
    public CommonResult findCourseCheckWithWait(Integer page, Integer limit, CourseCheckVo courseCheckVo) {
        Page<CourseCheckVo> objectPage = new Page<>(page,limit);
        QueryWrapper<CourseCheckVo> objectQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(courseCheckVo.getTitle())){
            objectQueryWrapper.like("title",courseCheckVo.getTitle());
        }
        if (StringUtils.isNotEmpty(courseCheckVo.getStatus())){
            objectQueryWrapper.like("status",courseCheckVo.getStatus());
        }
        Page<CourseCheckVo> courseCheckWithWait = courseCheckDao.findCourseCheckWithWait(objectPage, objectQueryWrapper);
        List<CourseCheckVo> records = courseCheckWithWait.getRecords();
        List<Subject> subjects = courseSubjectDao.selectList(null);
        for (CourseCheckVo c:records) {
            String s="";
            String s1 = findcourseSubjectName(subjects, c.getSubjectId());
            String[] split = s1.split(">");
            for (int i = split.length-1; i >= 0; i--) {
                if (i!=0)
                    s += split[i]+">";
                else
                    s += split[i];

            }
            c.setSubjectName(s);
        }

        return new CommonResult(200,"查询成功",courseCheckWithWait);
    }

    @Override
    public CommonResult updateCourseCheck(CourseCheckVo courseCheckVo) {
        int i = courseCheckDao.updateCourseCheck(courseCheckVo);
        if (i>0){
            return new CommonResult(200,"审核成功","yes");
        }else {
            return new CommonResult(500,"审核失败","no");
        }

    }
}
